package com.mp.goods.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mp.common.Result;
import com.mp.goods.bean.Standard;
import com.mp.goods.dto.InsertStandardDTO;
import com.mp.goods.dto.SelectStandardDTO;
import com.mp.goods.dto.UpdateStandardDTO;
import com.mp.goods.mapper.StandardMapper;
import com.mp.goods.service.StandardService;
import com.mp.goods.vo.StandardVO;
import com.mp.thread.StandardThread;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 规格表 前端控制器
 * </p>
 *
 * @author notejava
 * @since 2020-07-10
 */
@RestController
@RequestMapping("/goods/standard")
@Api(value = "规格模块Api文档",tags = "商品规格")
public class StandardController {

    @Resource
    public StandardService standardService;
    @Resource
    public StandardMapper standardMapper;


    @PostMapping(value ="/page")
    @ApiOperation(value="分页查询规格表")
    public Result selectPage(@RequestBody SelectStandardDTO model){
       return Result.success(standardService.selectPage(model));
    }

    @PostMapping(value="/insert")
    @ApiOperation(value="新增规格")
    public Result insert(@RequestBody InsertStandardDTO insertStandardDTO){
        return Result.success(standardService.insert(insertStandardDTO));
    }

    @GetMapping(value = "/delete/{standardId}")
    @ApiOperation(value ="根据id删除规格")
    public Result delete(@PathVariable Long standardId){
        return Result.success(standardService.delete(standardId));
    }

    @PostMapping(value ="/update")
    @ApiOperation(value = "通过id更新规格")
    public Result update(@RequestBody UpdateStandardDTO updateStandardDTO){
        return Result.success(standardService.updateById(updateStandardDTO));
    }

    @GetMapping("/test")
    public Result test(){
        Standard standard = standardMapper.selectById(1281627342075727874L);
        Standard std = new Standard();
        std.setId(1281627342075727874L);
        std.setVersion(standard.getVersion());
        StandardThread notejava1 = new StandardThread(standardMapper,std,"线程1");
        StandardThread notejava2= new StandardThread(standardMapper,std,"线程2");
        StandardThread notejava3 = new StandardThread(standardMapper,std,"线程3");
        Thread thread1 = new Thread(notejava1);
        Thread thread2 = new Thread(notejava2);
        Thread thread3 = new Thread(notejava3);
        thread1.start();
        thread2.start();
        thread3.start();
        return Result.success(null);
    }

    @GetMapping(value="/selectRelation")
    public Result selectRelation(){
        Page<Map<String,Object>> page = new Page(1,10);
        List<Map<String, Object>> maps = standardMapper.selectRelation(page);
        page.setRecords(maps);
        return Result.success(page);
    }


}

